#!/bin/bash
###############################################################################
##@Author Nicholas.Zhang
##@Date 2011-04-25
###############################################################################
#BASE_BIN_DIR=`dirname $0`
#. $BASE_BIN_DIR/functions.sh
. `pwd`/functions.sh

#========begin enviroment var define
export JAVA_HOME=${java_home}
export JETTY_HOME=${jetty_home}
export NGINX_HOME=${nginx_home}
export APP_HOME=${app_home}
export APP_WEBAPP_HOME=${app_webapp_home}
export APP_OUTPUT_HOME=$APP_HOME/output
export APP_LOG_HOME=${app_log_home}
export APP_PORT=${app_port}
export DEBUG_PORT=${debug_port}
export PRODUCTION_MODE=${production_mode}
export JMX_PORT=${jmx_port}

export LANG=zh_CN.utf8
export CHECK_STARTUP_URL="http://127.0.0.1:$APP_PORT/test.htm"
export STARTUP_SUCCESS_MSG="start jetty successfully"
export JAVA_OPTS=" -server -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
export JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n "
export JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=$JAVA_HOME/jre/lib/management/jmxremote.password -Dcom.sun.management.jmxremote.access.file=$JAVA_HOME/jre/lib/management/jmxremote.access "

if [ ! -e $JAVA_HOME ]; then
	failed "********************************************************************"
	failed "**Error: $JAVA_HOME not exist"
	failed "********************************************************************"    
	exit 1
fi

if [ ! -e $JETTY_HOME ]; then
	failed "********************************************************************"
	failed "**Error: $JETTY_HOME not exist."
	failed "********************************************************************"
	exit 1
fi
#========end enviroment var define

if [ $PRODUCTION_MODE = "run" ]; then
	#after the environment of online is all 64-bit,the below if and else judgement can be delete
	str=`file $JAVA_HOME/bin/java | grep 64-bit`
	if [ -n "$str" ]; then
		let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'`
        if [ $memTotal -gt 2500 ];then            
			JAVA_MEM_OPTS=" -Xmx1024m -Xms1024m -Xmn128m -XX:PermSize=96m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly "
		else 
			JAVA_MEM_OPTS=" -Xmx512m -Xms512m -Xmn64m -XX:PermSize=64m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection  -XX:+UseCMSInitiatingOccupancyOnly "
		fi
	else
		JAVA_MEM_OPTS=" -Xms512m -Xmx512m -XX:PermSize=64m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
	fi
	JAVA_OPTS=" $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_JMX_OPTS "
elif [ $PRODUCTION_MODE = "test" ]; then
	#JAVA_MEM_OPTS=" -Xms512m -Xmx512m -XX:MaxPermSize=64m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
	JAVA_MEM_OPTS=" -Xmx1024m -Xms1024m -Xmn128m -XX:PermSize=96m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly "
	JAVA_OPTS=" $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_JMX_OPTS "
elif [ $PRODUCTION_MODE = "dev" ]; then
	#we shuold reduce resource usage on developing mode
	JAVA_MEM_OPTS=" -Xms64m -Xmx1024m -XX:MaxPermSize=128m "
	JAVA_OPTS=" $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_JMX_OPTS $JAVA_DEBUG_OPTS "
else
	failed "********************************************************************"
	failed "**Error: \$PRODUCTION_MODE should be only: run, test, dev"
	failed "********************************************************************"    
	exit 1
fi

export JETTY_SERVER_DIR=" -Djetty.home=$JETTY_HOME "
JAVA_OPTS=" $JAVA_OPTS $JETTY_SERVER_DIR "

deploy_war(){
	if [ $PRODUCTION_MODE = "run" ]; then
		if [ -d "$APP_WEBAPP_HOME" ] ; then
			rm -rf  "$APP_WEBAPP_HOME"
			echo "Remove path:  $APP_WEBAPP_HOME"
		fi
		
		if [ ! -d "$APP_WEBAPP_HOME" ] ; then
			mkdir "$APP_WEBAPP_HOME"
			echo "Make path:  $APP_WEBAPP_HOME"
		fi
    	
		cd "$APP_WEBAPP_HOME"
		
  		if [ -f "$APP_HOME/guoguojie-web.war" ] ; then
			jar xf "$APP_HOME/guoguojie-web.war"
		elif [ -f "$APP_HOME/target/deploy/guoguojie-web.war" ] ; then
			jar xf "$APP_HOME/target/deploy/guoguojie-web.war"
		elif [ -f "$APP_OUTPUT_HOME/web.war" ] ; then
			jar xf "$APP_OUTPUT_HOME/web.war"
		else
           	failed "********************************************************************"
    		failed "**Error: guoguojie-web.war not exist."
    		failed "********************************************************************"    
    		exit 1
		fi  	    
	else
		if [ -d  "$APP_WEBAPP_HOME" ] ; then
        	rm -rf  "$APP_WEBAPP_HOME"
            echo "Remove path:  $APP_WEBAPP_HOME"
        fi
		
		if [ ! -d "$APP_WEBAPP_HOME" ] ; then
			mkdir "$APP_WEBAPP_HOME"
			echo "Make path:  $APP_WEBAPP_HOME"
		fi
		
		cd "$APP_WEBAPP_HOME"
		
  		if [ -f "$APP_HOME/guoguojie-web.war" ] ; then
			jar xf "$APP_HOME/guoguojie-web.war"
		elif [ -f "$APP_HOME/target/deploy/guoguojie-web.war" ] ; then
			jar xf "$APP_HOME/target/deploy/guoguojie-web.war"
		elif [ -f "$APP_OUTPUT_HOME/web.war" ] ; then
			jar xf "$APP_OUTPUT_HOME/web.war"
		else
           	failed "********************************************************************"
    		failed "**Error: guoguojie-web.war not exist."
    		failed "********************************************************************"    
    		exit 1
		fi
	fi
}

copy_jetty_conf(){
	cp  "$APP_HOME/conf/jetty/jetty.xml"  "$JETTY_HOME/etc/jetty.xml"
	cp  "$APP_HOME/conf/jetty/webdefault.xml"  "$JETTY_HOME/etc/webdefault.xml"
	cp  "$APP_HOME/conf/jetty/fileserver.xml"  "$JETTY_HOME/contexts/fileserver.xml"
	cp  "$APP_HOME/conf/jetty/guoguojie.xml"  "$JETTY_HOME/contexts/guoguojie.xml"
	if [ ! -d "$JETTY_HOME/contexts/fileserver.d" ] ; then
		mkdir "$JETTY_HOME/contexts/fileserver.d"
	fi
	if [ ! -d "$JETTY_HOME/contexts/guoguojie.d" ] ; then
		mkdir "$JETTY_HOME/contexts/guoguojie.d"
	fi
	cp -r "$APP_HOME/conf/jetty/override-web.xml"  "$JETTY_HOME/contexts/fileserver.d/override-web.xml"
	cp -r "$APP_HOME/conf/jetty/override-web.xml"  "$JETTY_HOME/contexts/guoguojie.d/override-web.xml" 
}

copy_nginx_conf(){
	cp  "$APP_HOME/conf/nginx/nginx.conf"  "$NGINX_HOME/conf/nginx.conf"
}

deploy(){
    copy_jetty_conf
    deploy_war
}

